﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using SaveMeWCF;
using Backend;
using DBLayer;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace SaveMeWCF
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
    public class SaveMe : ISaveMe
    {
        public void MarkCancelRequestAsReviewed(string id)
        {
            DBTaxiRequest dbTaxiRequest = new DBTaxiRequest();
            TaxiRequest taxiRequest = dbTaxiRequest.GetTaxiRequest(int.Parse(id));

            if (taxiRequest.Status == 4)
            {
                taxiRequest.Status = 5;

                dbTaxiRequest.UpdateTaxiRequest(int.Parse(id), taxiRequest.Status);
            }
        
        
        }

        public void CancelTaxiRequestFromPhone(string id)
        {

            DBTaxiRequest dbTaxiRequest = new DBTaxiRequest();
            TaxiRequest taxiRequest = dbTaxiRequest.GetTaxiRequest(int.Parse(id));

            if (taxiRequest.Status != 5)
            {
                taxiRequest.Status = 4;

                dbTaxiRequest.UpdateTaxiRequest(int.Parse(id), taxiRequest.Status);
            }
        
        }

        public int SendMyLocation(string location)
        {
            int id = GetMax.GetMaxId() + 1;

            TaxiRequest taxiRequest = new TaxiRequest();
            taxiRequest.Id = id;
            //taxiRequest.Date = DateTime.Now.ToString("yyyyMMddHHmmss");
            //taxiRequest.Date = "2013/05/05 04:04:04";
            taxiRequest.Status = 1;
            taxiRequest.Address = location;
            taxiRequest.PhoneNr = "9999999";

            DBTaxiRequest dbTaxiRequest = new DBTaxiRequest();
            dbTaxiRequest.SaveTaxiRequest(taxiRequest);


            return id;
            
        }


        public TaxiRequest [] GetTaxiRequestsByStatus()
        {
            
            
            DBTaxiRequest dbTaxiRequest = new DBTaxiRequest();
            List<TaxiRequest> taxiRequests = dbTaxiRequest.getTaxiRequestsByStatus(1);
            taxiRequests.AddRange(dbTaxiRequest.getTaxiRequestsByStatus(2));
            taxiRequests.AddRange(dbTaxiRequest.getTaxiRequestsByStatus(4));

            foreach (TaxiRequest taxiRequest in taxiRequests)
            {
                try{
                    User user = dbTaxiRequest.GetUser(taxiRequest.PhoneNr);
                    if (user.HomeAddress != null)
                    {
                        taxiRequest.HomeAddress = user.HomeAddress;
                    }
                    else
                    {
                        taxiRequest.HomeAddress = "     ";
                    }

                }
                catch{
                    taxiRequest.HomeAddress = "ingen hjem adresse"; 
                }
            }

            //XmlDocument doc = new XmlDocument();// Create the XML Declaration, and append it to XML document
            //XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", null, null);
            //doc.AppendChild(dec);// Create the root element
            //XmlElement root = doc.CreateElement("Library");
            //doc.AppendChild(root);

            //// Create Books
            //// Note that to set the text inside the element,
            //// you use .InnerText instead of .Value (which will throw an exception).
            //// You use SetAttribute to set attribute
            //XmlElement book = doc.CreateElement("Book");
            //book.SetAttribute("BookType", "Hardcover");
            //XmlElement title = doc.CreateElement("Title");
            //title.InnerText = "Door Number Three";
            //XmlElement author = doc.CreateElement("Author");
            //author.InnerText = "O'Leary, Patrick";
            //book.AppendChild(title);
            //book.AppendChild(author);
            //root.AppendChild(book);

            //string xmlOutput = doc.OuterXml;


 //           XmlSerializer xsSubmit = new XmlSerializer(typeof(TaxiRequest));
 //TaxiRequest taxiRequest = taxiRequests.
 //           var subReq = new MyObject();
 //StringWriter sww = new StringWriter();
 //XmlWriter writer = XmlWriter.Create(sww);
 //xsSubmit.Serialize(writer, subReq);
 //var xml = sww.ToString()
 //           return xmlOutput;


           // int count = taxiRequests.Count;

           // string[][] something = new string[2000][];
           // int counter = 0;


            //foreach (TaxiRequest taxiRequest in taxiRequests)
            //{
            //    string[] text = new string[5] { 
            //    taxiRequest.Id.ToString(), taxiRequest.PhoneNr,taxiRequest.Address ,
            //        taxiRequest.Date ,taxiRequest.Status.ToString()};
            //        something[counter] = text;
            //    counter++;
            //}

            //return something;

            return taxiRequests.ToArray();
            

        }





        public int GetMyTaxiStatus(string id)
        {
            int status;

            DBTaxiRequest dbTaxiRequest = new DBTaxiRequest();

            TaxiRequest taxiRequest=dbTaxiRequest.GetTaxiRequest(int.Parse(id));

            status = taxiRequest.Status;

            return status;

        }


        public void UpdateTaxiRequest(string id)
        {

            DBTaxiRequest dbTaxiRequest = new DBTaxiRequest();
            TaxiRequest taxiRequest = dbTaxiRequest.GetTaxiRequest(int.Parse(id));
            if(!(taxiRequest.Status>3))
            dbTaxiRequest.UpdateTaxiRequest(int.Parse(id), taxiRequest.Status+1);
        
        }

        public void UpdateTaxiRequestWithEstimatedTime(string id, string estimatedTime)
        {
            DBTaxiRequest dbTaxiRequest = new DBTaxiRequest();
            dbTaxiRequest.UpdateTaxiRequestWithEstimatedTime(int.Parse(id), estimatedTime);
        }

        public void UpdateTaxiRequestWithPlateNr(string id, string plateNr)
        {
            DBTaxiRequest dbTaxiRequest = new DBTaxiRequest();
            dbTaxiRequest.UpdateTaxiRequestWithPlateNr(int.Parse(id), plateNr);
        }



        public TimeAndPlate GetEstimatedTimeAndPlateNr(string id)
        {
           
            string[] timeAndPlates = new string[2];

            DBTaxiRequest dbTaxiRequest = new DBTaxiRequest();
            TaxiRequest taxiRequest = dbTaxiRequest.GetTaxiRequest(int.Parse(id));

            timeAndPlates[0] = taxiRequest.EstimatedTime;
            timeAndPlates[1] = taxiRequest.PlateNr;

            TimeAndPlate timeAndPlatE = new TimeAndPlate(taxiRequest.EstimatedTime, taxiRequest.PlateNr);
            return timeAndPlatE;
        }


        public string GetPlateNr(string id)
        {
            DBTaxiRequest dbTaxiRequest = new DBTaxiRequest();
            TaxiRequest taxiRequest = dbTaxiRequest.GetTaxiRequest(int.Parse(id));

            return taxiRequest.PlateNr;
        }

        public string GetEstimatedTime(string id)
        {
            DBTaxiRequest dbTaxiRequest = new DBTaxiRequest();
            TaxiRequest taxiRequest = dbTaxiRequest.GetTaxiRequest(int.Parse(id));

            return taxiRequest.EstimatedTime;
        }


        public bool SaveNewUser(string phoneNr, string homeAddress)
        {
            DBTaxiRequest dbTaxiRequest = new DBTaxiRequest();
            User newUser = new User();
            newUser.PhoneNr = phoneNr;
            newUser.HomeAddress = homeAddress;

            bool isSaved=dbTaxiRequest.SaveUser(newUser);

            return isSaved;
        }

        public int SendMyLocationWithPhoneNr(string location,string phoneNr)
        {
            int id = GetMax.GetMaxId() + 1;

            TaxiRequest taxiRequest = new TaxiRequest();
            taxiRequest.Id = id;
            //taxiRequest.Date = DateTime.Now.ToString("yyyyMMddHHmmss");
            //taxiRequest.Date = "2013/05/05 04:04:04";
            taxiRequest.Status = 1;
            taxiRequest.Address = location;
            taxiRequest.PhoneNr = phoneNr;

            DBTaxiRequest dbTaxiRequest = new DBTaxiRequest();
            dbTaxiRequest.SaveTaxiRequest(taxiRequest);


            return id;

        }

      
    }
}
